﻿//==============================================================================
// File: CommonRules.cs
// Created: 2010-05-10
// Author: Piotr Włodek
//==============================================================================
// This file is a part of MAMMOTH project.
// Copyright (C) 2010 AGH University of Science and Technology, Krakow.
// https://caribou.iisg.agh.edu.pl/trac/mammoth
//
// Mammoth is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// 
// Mammoth is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Lesser General Public License for more details.
// 
// You should have received a copy of the GNU Lesser General Public License
// along with WpfRcp. If not, see http://www.gnu.org/licenses/.
//==============================================================================

using System;
using System.Collections.Specialized;
using Res = WpfRcp.PropertyModel.Properties.Resources;

namespace WpfRcp.PropertyModel.Validation
{
    /// <summary>
    /// Contains common validation rules.
    /// </summary>
    public static class CommonRules
    {
        /// <summary>
        /// Represents a validation rule which checks if the given property is not null.
        /// </summary>
        public static ValidationResult NotNull(object value, ValidationArgs e)
        {
            return value == null
                       ? new ValidationResult(false, string.Format(Res.ValidationNotNull, e.Property.Name))
                       : ValidationResult.Valid;
        }

        /// <summary>
        /// Represents a validation rule which checks if the given property does not have a default value.
        /// </summary>
        public static ValidationResult NotDefault<T>(object value, ValidationArgs e)
        {
            if (value == null || value.Equals(default(T)))
                return new ValidationResult(false, string.Format(Res.ValidationNotDefault, e.Property.Name));

            return ValidationResult.Valid;
        }

        public static ValidationResult NotifyOnValidationError(object value, ValidationArgs e)
        {
            var newValue = value as IValidatable;
            if (newValue != null)
            {
                if (newValue.IsValid == false)
                {
                    return new ValidationResult(false, string.Format(
                        Res.ValidationValueIsInvalid, e.Property.Name));
                }
            }

            return ValidationResult.Valid;
        }

        public static ValidationResult StringRequired(object value, ValidationArgs e)
        {
            var str = (string) value;
            if (string.IsNullOrEmpty(str))
            {
                return new ValidationResult(false,
                    string.Format(Res.ValidationStringRequired, e.Property.Name));
            }

            return ValidationResult.Valid;
        }

        public static ValidationResult NotifiableCollectionRequired(object value, ValidationArgs e)
        {
            var notifiableCollection = value as INotifyCollectionChanged;
            if (value != null && notifiableCollection == null)
            {
                return new ValidationResult(false,
                    string.Format(Res.ValidationNotifiableCollectionRequired, e.Property.Name));
            }

            return ValidationResult.Valid;
        }

        [Serializable]
        public sealed class IntegerRangeValidationRule : ValidationRule
        {
            public int MinValue { get; private set; }

            public int MaxValue { get; private set; }

            public IntegerRangeValidationRule(int minValue, int maxValue)
            {
                MaxValue = maxValue;
            }

            protected override ValidationResult DoValidate(object value, ValidationArgs e)
            {
                var i = (int)value;

                if (i > MaxValue)
                {
                    return new ValidationResult(false,
                        string.Format(Res.ValidationIntegerMaxValue, e.Property.Name, MaxValue));
                }

                if (i < MinValue)
                {
                    return new ValidationResult(false,
                        string.Format(Res.ValidationIntegerMinValue, e.Property.Name, MinValue));
                }

                return ValidationResult.Valid;
            }
        }

        [Serializable]
        public sealed class IntegerMaxValueValidationRule : ValidationRule
        {
            public int MaxValue { get; private set; }

            public IntegerMaxValueValidationRule(int maxValue)
            {
                MaxValue = maxValue;
            }
            
            protected override ValidationResult DoValidate(object value, ValidationArgs e)
            {
                var i = (int)value;

                if (i > MaxValue)
                {
                    return new ValidationResult(false,
                        string.Format(Res.ValidationIntegerMaxValue, e.Property.Name, MaxValue));
                }

                return ValidationResult.Valid;
            }
        }

        [Serializable]
        public sealed class DoubleMaxValueValidationRule : ValidationRule
        {
            public double MaxValue { get; private set; }

            public DoubleMaxValueValidationRule(double maxValue)
            {
                MaxValue = maxValue;
            }

            protected override ValidationResult DoValidate(object value, ValidationArgs e)
            {
                var i = (int)value;

                if (i > MaxValue)
                {
                    return new ValidationResult(false,
                        string.Format(Res.ValidationIntegerMaxValue, e.Property.Name, MaxValue));
                }

                return ValidationResult.Valid;
            }
        }

        [Serializable]
        public sealed class IntegerMinValueValidationRule : ValidationRule
        {
            public int MinValue { get; private set; }

            public IntegerMinValueValidationRule(int minValue)
            {
                MinValue = minValue;
            }

            protected override ValidationResult DoValidate(object value, ValidationArgs e)
            {
                var i = (int)value;

                if (i < MinValue)
                {
                    return new ValidationResult(false,
                        string.Format(Res.ValidationIntegerMinValue, e.Property.Name, MinValue));
                }

                return ValidationResult.Valid;
            }
        }

        [Serializable]
        public sealed class DoubleMinValueValidationRule : ValidationRule
        {
            public double MinValue { get; private set; }

            public DoubleMinValueValidationRule(double minValue)
            {
                MinValue = minValue;
            }

            protected override ValidationResult DoValidate(object value, ValidationArgs e)
            {
                var i = (double)value;

                if (i < MinValue)
                {
                    return new ValidationResult(false,
                        string.Format(Res.ValidationIntegerMinValue, e.Property.Name, MinValue));
                }

                return ValidationResult.Valid;
            }
        }
    }
}